package com.kaily.image.utils;

import android.Manifest;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

/**
 * TODO:权限工具类
 * Created by weiang on 2017/6/9.
 */
@SuppressWarnings("ALL")
public class PermissionUtil {
    public static final int REQUEST_CODE_PERMISSION_LOCATION = 2001;
    public static final int REQUEST_CODE_PERMISSION_CAMERA = 2002;
    public static final int REQUEST_CODE_PERMISSION_SMS = 2003;
    public static final int REQUEST_CODE_PERMISSION_READ_STORAGE = 2004;
    public static final int REQUEST_CODE_PERMISSION_CAMERA_READ_STORAGE = 2005;
    public static final int REQUEST_CODE_PERMISSION_ADAPTER_STORAGE = 2006;
    public static final int REQUEST_CODE_PERMISSION_PHONE_LINK_MAN = 2007;
    public static final int REQUEST_CODE_PERMISSION_PHONE = 2008;
    public static final int REQUEST_CODE_PERMISSION_W_STORAGE = 2009;
    public static final int REQUEST_CODE_PERMISSION_SHAREFRIEND = 2011;

    public static final int REQUEST_CODE_PERMISSION_POLICY = 2018;

    public static final int REQUEST_CODE_PERMISSION_520_READ_STORAGE = 2012;

    public static final int PERMISSION_RESULT_ALLOW = 1;//权限被允许
    public static final int PERMISSION_RESULT_REFUSE = 2;//权限被拒绝
    private static final int PERMISSION_RESULT_NO = 3;//非请求权限

    //定位权限
    public static boolean permissionLocation(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermissionLocation = ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION);
            int checkPermissionCorarse = ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION);

            if (checkPermissionLocation != PackageManager.PERMISSION_GRANTED || checkPermissionCorarse != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity
                        , new String[]
                                {
                                        Manifest.permission.ACCESS_FINE_LOCATION
                                        , Manifest.permission.ACCESS_COARSE_LOCATION
                                }
                        , REQUEST_CODE_PERMISSION_LOCATION);
                //Logger.d("test", "开始询问定位权限");
                return false;
            } else {
                //Logger.d("test", "已经获取定位权限");
                return true;
            }
        } else {
            //Logger.d("test", "非6.0版本");
            return true;
        }
    }

    public static boolean isLocationPermission(String[] permissions) {
        return !(permissions == null || permissions.length < 1) && permissions[0].equals(Manifest.permission.ACCESS_FINE_LOCATION);

    }

    //获取定位权限结果
    public static boolean getPermissionLocationResult(String[] permissions, int[] grantResults) {
        return permissions[0].equals(Manifest.permission.ACCESS_FINE_LOCATION)
                && grantResults[0] == PackageManager.PERMISSION_GRANTED
                && permissions[1].equals(Manifest.permission.ACCESS_COARSE_LOCATION)
                && grantResults[1] == PackageManager.PERMISSION_GRANTED;

    }

    //相机权限
    public static boolean permissionCamera(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermissionCamera = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
            if (checkPermissionCamera != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_PERMISSION_CAMERA);
                return false;
            } else {
                return true;
            }
        }

        return true;
    }

    public static boolean isCallPermission(String[] permissions) {
        return !(permissions == null || permissions.length < 1) && permissions[0].equals(Manifest.permission.CALL_PHONE);
    }


    public static boolean isWritePermission(String[] permissions) {
        return !(permissions == null || permissions.length < 1) && permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }


    public static boolean isCarmeraPermission(String[] permissions) {
        return !(permissions == null || permissions.length < 1) && permissions[0].equals(Manifest.permission.CAMERA);

    }

    //获取相机权限限结果
    public static boolean getPermissionCameraResult(String[] permissions, int[] grantResults) {
        return permissions[0].equals(Manifest.permission.CAMERA) && grantResults[0] == PackageManager.PERMISSION_GRANTED;

    }

    //短信接收、读取
    public static boolean permissionSMS(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermissionReadSMS = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_SMS);
            int checkPermissionReceiveSMS = ContextCompat.checkSelfPermission(activity, Manifest.permission.RECEIVE_SMS);

            if (checkPermissionReadSMS != PackageManager.PERMISSION_GRANTED || checkPermissionReceiveSMS != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity
                        , new String[]
                                {
                                        Manifest.permission.RECEIVE_SMS
                                        , Manifest.permission.READ_SMS
                                        , Manifest.permission.RECEIVE_WAP_PUSH
                                }
                        , REQUEST_CODE_PERMISSION_SMS);

                return false;
            }

            return true;
        } else {
            return true;
        }
    }

    //读取手机存储
    public static boolean permissionReadStorage(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermissionWrite = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
            if (checkPermissionWrite != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity
                        , new String[]
                                {
                                        Manifest.permission.READ_EXTERNAL_STORAGE
                                }
                        , REQUEST_CODE_PERMISSION_READ_STORAGE);

                return false;
            } else {
                return true;
            }
        }
        return true;
    }


    //读取手机存储
    public static boolean permission520ReadStorage(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermissionWrite = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
            if (checkPermissionWrite != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity
                        , new String[]
                                {
                                        Manifest.permission.READ_EXTERNAL_STORAGE
                                }
                        , REQUEST_CODE_PERMISSION_520_READ_STORAGE);

                return false;
            } else {
                return true;
            }
        }
        return true;
    }


    public static boolean isReadStoragePermission(String[] permissions) {
        return !(permissions == null || permissions.length < 1) && permissions[0].equals(Manifest.permission.READ_EXTERNAL_STORAGE);

    }

    //获取手机存储权限限结果
    public static boolean getPermissionReadStorageResult(String[] permissions, int[] grantResults) {
        return permissions[0].equals(Manifest.permission.READ_EXTERNAL_STORAGE) && grantResults[0] == PackageManager.PERMISSION_GRANTED;
    }

    //相机权限 读取文件
    public static boolean permissionCameraAndReadStorage(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermissionCamera = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
            int checkPermissionReadStorage = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);

            if (checkPermissionCamera != PackageManager.PERMISSION_GRANTED || checkPermissionReadStorage != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity
                        , new String[]
                                {
                                        Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE
                                }
                        , REQUEST_CODE_PERMISSION_CAMERA_READ_STORAGE);

                return false;
            } else {
                return true;
            }
        }

        return true;
    }

    //相机权限 读取文件
    public static boolean permissionCameraAndReadStorage2(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermissionCamera = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
            int checkPermissionReadStorage = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
            int checkPermissionWriteStorage = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

            if (checkPermissionCamera != PackageManager.PERMISSION_GRANTED || checkPermissionReadStorage != PackageManager.PERMISSION_GRANTED || checkPermissionWriteStorage != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity
                        , new String[]
                                {
                                        Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE
                                }
                        , REQUEST_CODE_PERMISSION_CAMERA_READ_STORAGE);

                return false;
            } else {
                return true;
            }
        }

        return true;
    }


    /**
     * 检查本地是否有存储权限
     *
     * @param activity
     * @return
     */
    public static boolean isWritePermission(Activity activity) {
        int checkPermissionReadStorage = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
        int checkPermissionWriteStorage = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        boolean result = (checkPermissionReadStorage == PackageManager.PERMISSION_GRANTED && checkPermissionWriteStorage == PackageManager.PERMISSION_GRANTED);
        return result;
    }


    public static boolean isCarmeraAndReadStoragePermission(String[] permissions) {
        return !(permissions == null || permissions.length < 1) && permissions[0].equals(Manifest.permission.CAMERA) && permissions[1].equals(Manifest.permission.READ_EXTERNAL_STORAGE);

    }

    //获取相机权限限结果
    public static boolean getPermissionCarmeraAndReadStorageResult(String[] permissions, int[] grantResults) {
        return permissions[0].equals(Manifest.permission.CAMERA)
                && grantResults[0] == PackageManager.PERMISSION_GRANTED
                && permissions[1].equals(Manifest.permission.READ_EXTERNAL_STORAGE)
                && grantResults[1] == PackageManager.PERMISSION_GRANTED
                && permissions[2].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                && grantResults[2] == PackageManager.PERMISSION_GRANTED;
    }

    public static boolean isStoragePermission(String[] permissions) {
        return !(permissions == null || permissions.length < 2) && permissions[0].equals(Manifest.permission.READ_EXTERNAL_STORAGE) && permissions[1].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE);

    }


    // 读写文件
    public static boolean permissionAdapterStorage(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermissionReadStorage = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
            int checkPermissionWriteStorage = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

            if (checkPermissionReadStorage != PackageManager.PERMISSION_GRANTED
                    || checkPermissionWriteStorage != PackageManager.PERMISSION_GRANTED
            ) {
                ActivityCompat.requestPermissions(activity
                        , new String[]
                                {
                                        Manifest.permission.READ_EXTERNAL_STORAGE
                                        , Manifest.permission.WRITE_EXTERNAL_STORAGE
                                }
                        , REQUEST_CODE_PERMISSION_ADAPTER_STORAGE);
                return false;
            } else {
                return true;
            }
        }
        return true;
    }


    //静音权限
    public static boolean permissionNoticationPolicy(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermissionNoticationPolicy = ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_NOTIFICATION_POLICY);
            if (checkPermissionNoticationPolicy != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity
                        , new String[]{Manifest.permission.ACCESS_NOTIFICATION_POLICY}
                        , REQUEST_CODE_PERMISSION_POLICY);
                return false;
            } else {
                return true;
            }
        }
        return true;
    }

    //静音权限
    public static boolean checkPermissionNoticationP(Activity activity) {
        NotificationManager notificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && !notificationManager.isNotificationPolicyAccessGranted()) {
//            Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
//            activity.startActivity(intent);
            return false;
        } else {
            return true;
        }
    }

    // 读写文件
    public static boolean permissionAdapterStorage(Activity activity, int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermissionReadStorage = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
            int checkPermissionWriteStorage = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (checkPermissionReadStorage != PackageManager.PERMISSION_GRANTED || checkPermissionWriteStorage != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity
                        , new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}
                        , requestCode);
                return false;
            } else {
                return true;
            }
        }
        return true;
    }


    //读写文件
    public static boolean permissionCameraAndStorage(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermissionReadStorage = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
            int checkPermissionWriteStorage = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

            if (checkPermissionReadStorage != PackageManager.PERMISSION_GRANTED
                    || checkPermissionWriteStorage != PackageManager.PERMISSION_GRANTED
            ) {
                ActivityCompat.requestPermissions(activity
                        , new String[]
                                {
                                        Manifest.permission.READ_EXTERNAL_STORAGE
                                        , Manifest.permission.WRITE_EXTERNAL_STORAGE
                                }
                        , REQUEST_CODE_PERMISSION_W_STORAGE);
                return false;
            } else {
                return true;
            }
        }
        return true;
    }


    //获取相机权限限结果
    public static boolean getPermissionStorageResult(String[] permissions, int[] grantResults) {
        if (permissions != null && grantResults != null && permissions.length > 0 && grantResults.length > 0) {
            return
                    permissions[0].equals(Manifest.permission.READ_EXTERNAL_STORAGE)
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED
                            && permissions[1].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            && grantResults[1] == PackageManager.PERMISSION_GRANTED;
        } else {
            return false;
        }

    }


    public static boolean isCarmeraAndStoragePermission(String[] permissions) {
        return !(permissions == null || permissions.length < 3)
                && permissions[0].equals(Manifest.permission.CAMERA)
                && permissions[1].equals(Manifest.permission.READ_EXTERNAL_STORAGE)
                && permissions[2].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    //获取相机权限限结果
    public static boolean getPermissionCarmeraAndStorageResult(String[] permissions, int[] grantResults) {

        return permissions[0].equals(Manifest.permission.CAMERA)
                && grantResults[0] == PackageManager.PERMISSION_GRANTED

                && permissions[1].equals(Manifest.permission.READ_EXTERNAL_STORAGE)
                && grantResults[1] == PackageManager.PERMISSION_GRANTED

                && permissions[2].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                && grantResults[2] == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * 是否有手机通讯录权限
     *
     * @param permissions 权限
     * @return 布尔值
     */
    public static boolean isPhoneLinkmanPermission(String[] permissions) {
        return !(permissions == null || permissions.length < 1) && permissions[0].equals(Manifest.permission.READ_CONTACTS) && permissions[1].equals(Manifest.permission.WRITE_CONTACTS);

    }

    /**
     * 手机通讯录权限
     *
     * @param activity 上下文环境
     * @return 布尔值
     */
    public static boolean permissionPhoneLinkman(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int readContacts = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_CONTACTS);
            int writeContacts = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_CONTACTS);

            if (readContacts != PackageManager.PERMISSION_GRANTED
                    || writeContacts != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity
                        , new String[]{
                                Manifest.permission.READ_CONTACTS
                                , Manifest.permission.WRITE_CONTACTS
                        }
                        , REQUEST_CODE_PERMISSION_PHONE_LINK_MAN);

                return false;
            } else {
                return true;
            }
        }

        return true;
    }

    /**
     * 获取手机通讯录权限结果
     *
     * @param permissions  权限集合
     * @param grantResults *
     * @return 布尔值
     */
    public static boolean getPermissionPhoneLinkmanResult(String[] permissions, int[] grantResults) {
        return permissions[0].equals(Manifest.permission.READ_CONTACTS)
                && grantResults[0] == PackageManager.PERMISSION_GRANTED

                && permissions[1].equals(Manifest.permission.WRITE_CONTACTS)
                && grantResults[1] == PackageManager.PERMISSION_GRANTED;

    }

    //电话权限
    public static boolean permissionPhone(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkPermissionPhone = ContextCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE);
            if (checkPermissionPhone != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                        activity
                        , new String[]{Manifest.permission.CALL_PHONE}
                        , REQUEST_CODE_PERMISSION_PHONE);
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }

    //获取电话权限结果
    public static int getPermissionPhoneResult(int requestCode, String[] permissions, int[] grantResults) {
        if (REQUEST_CODE_PERMISSION_PHONE != requestCode) {
            return PERMISSION_RESULT_NO;
        }

        if (permissions == null || permissions.length < 1) {
            return PERMISSION_RESULT_NO;
        }

        if (permissions[0].equals(Manifest.permission.CALL_PHONE)) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                return PERMISSION_RESULT_ALLOW;
            } else {
                return PERMISSION_RESULT_REFUSE;
            }
        }

        return PERMISSION_RESULT_NO;
    }

}

